
	var postOK = false;
	var post2OK = false;
	var nameOK = false;
	var passwordOK = false;
	var repasswordOK = false;
	var emailOK = false;
	var phoneOK = false;
	var birthdayOK = false;

	function chkname(){
		var nameStr = $("#name")[0].value;
		var nameLen = nameStr.length;
		if( nameLen<3 || nameLen>10 )
		{
			$("#nametip").html("<em style='color:red'>用户名长度应该为3到10间</em>");
			nameOK = false;
		}
		else
		{
			postOK = false;
			$.post("?a=chkname",{ name:nameStr},
			function(data,status){
				if(data == "ok")
				{
					$("#nametip").html("<em style='color:green'>用户名可用</em>");
					nameOK = true;
				}
				else if(data == "has")
				{
					$("#nametip").html("<em style='color:red'>用户名被占用</em>");
					nameOK = false;
				}
				else
				{
					$("#nametip").html("<em style='color:red'>别开玩笑了亲a</em>");
					nameOK = false;
				}
				postOK = true;
			});
		}
	};

	function chkpost() {
		if( postOK && post2OK )
		{
			if( nameOK && passwordOK && repasswordOK && emailOK && phoneOK && birthdayOK )
			{
				$("#registerBtn")[0].disabled = true;
				$("#registerBtn")[0].value = "正在注册……" ;
				var nameStr = $("#name")[0].value;
				var passwordStr = $("#password")[0].value;
				var repasswordStr = $("#repassword")[0].value;
				var emailStr = $("#email")[0].value;
				var phoneStr = $("#phone")[0].value;
				var birthdayStr = $("#birthday")[0].value;
				var sexStr = $("#sex")[0].value;

				$.post("?a=adduser",
				{
					name:		nameStr,
					password:	passwordStr,
					email:		emailStr,
					phone:		phoneStr,
					birthday:	birthdayStr,
					sex:		sexStr
				},
				function(data,status){
					if( data == "ok" )
					{
						$("#registerBtn")[0].value = "正在发送激活邮件……";
						$.post("?a=sendemail&name="+nameStr,
						{
							password:	passwordStr,
							email:		emailStr
						},
						function(data,status) {
							setTimeout("parent.location = '?a=login'",1000);
							delCookie("name");
							$("#registerBtn")[0].value = "邮件发送完成，正在为您跳转……";
						});
					}
					else
					{
						alert("注册失败，错误信息为\n"+data);
						$("#registerBtn")[0].disabled = false;
						$("#registerBtn")[0].value = "注册" ;
					}
				});
			}
			else
			{
				alert("请检查书写~");
				var str = "error:";
				if( !nameOK ) str+="nameOK";
				if( !passwordOK ) str+="passwordOK";
				if( !repasswordOK ) str+="repasswordOK";
				if( !emailOK ) str+="emailOK";
				if( !phoneOK ) str+="phoneOK";
				if( !birthdayOK ) str+="birthdayOK";
			}
		}
		else
		{
			setTimeout("chkpost()",100);
		}
	};

	function chkpassword(){
		var passwordStr = $("#password")[0].value;
		var passwordLen = passwordStr.length;
		if( passwordLen<6 || passwordLen>16)
		{
			$("#passwordtip").html("<em style='color:red'>密码长度应该为6到16间</em>");
			passwordOK = false;
		}
		else
		{
			$("#passwordtip").html("<em style='color:green'>密码符合条件</em>");
			passwordOK = true;
		}
	};

	function chkrepassword(){
		var passwordStr = $("#password")[0].value;
		var repasswordStr = $("#repassword")[0].value;

		if( passwordOK )
		{
			if( passwordStr == repasswordStr )
			{
				$("#repasswordtip").html("<em style='color:green'>两次密码一致</em>");
				repasswordOK = true;
			}
			else
			{
				$("#repasswordtip").html("<em style='color:red'>两次密码不一致</em>");
				repasswordOK = false;
			}
		}
	};

	function chkemail(){
		var emailStr = $("#email")[0].value;
		if( emailStr.match(/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/) == null )
		{
			$("#emailtip").html("<em style='color:red'>email格式错误</em>");
			emailOK = false;
		}
		else
		{
			$("#emailtip").html("<em style='color:green'>正在检查email……</em>");
			
			post2OK = false;
			$.post("?a=chkemail",{ email:emailStr},
			function(data,status){
				if(data == "ok")
				{
					$("#emailtip").html("<em style='color:green'>email可用</em>");
					emailOK = true;
				}
				else if(data == "has")
				{
					$("#emailtip").html("<em style='color:red'>email已注册过</em>");
					emailOK = false;
				}
				else
				{
					$("#emailtip").html("<em style='color:red'>别开玩笑了</em>");
					emailOK = false;
				}
				post2OK = true;
			});
		}
	};

	function chkphone(){
		var phoneStr = $("#phone")[0].value;
		var phoneLen = phoneStr.length;
		if( phoneStr.match("^6[0-9]*$") == null || parseInt((phoneLen+1)/2) != 3)
		{
			$("#phonetip").html("<em style='color:red'>短号格式错误</em>");
			phoneOK = false;
		}
		else
		{
			$("#phonetip").html("<em style='color:green'>短号格式正确</em>");
			phoneOK = true;
		}
	};

	function chkbirthday(){
		var birthdayStr = $("#birthday")[0].value;
		var birthdayLen = birthdayStr.length;

		if( birthdayStr.match("19[8-9][0-9][0-1][0-9][0-9][0-9]$") == null )
		{
			$("#birthdaytip").html("<em style='color:red'>生日格式错误</em>");
			birthdayOK = false;
		}
		else
		{
			$("#birthdaytip").html("<em style='color:green'>生日格式正确</em>");
			birthdayOK = true;
		}
	};

$(document).ready(function(){

	$("#name").blur( chkname );
	$("#password").blur( chkpassword );
	$("#repassword").blur( chkrepassword );
	$("#email").blur( chkemail );
	$("#phone").blur( chkphone );
	$("#birthday").blur( chkbirthday );

	$("#registerBtn")[0].disabled = false;
	$("#registerBtn")[0].value = "注册" ;

	$("#registerBtn").click(function(){
		chkname();
		chkpassword();
		chkrepassword();
		chkemail();
		chkbirthday();
		chkphone();
		chkpost();
	});
});